-module(parallelserver).
-export([start/0, loop/1]).

start() ->
	{ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 0}, {reuseaddr, true}, {active, true}]),
	spawn(fun() -> par_loop(Listen) end).
	
par_loop(Listen) ->
	{ok, Socket} = gen_tcp:accept(Listen),
	io:format("Socket accepted: ~w~n", [Socket]),
	spawn(fun() -> par_loop(Listen) end),
	loop(Socket).
	
loop(Socket) ->
	receive
		{tcp, Socket, Bin} ->
			io:format("Server received binary: ~w~n", [Bin]),
			Str = binary_to_list(Bin),
			io:format("Server (unpacked) ~p~n", [Str]),
			%Reply = "~n....response",
			%io:format("Server replying...~p~n", [Reply]),
			%gen_tcp:send(Socket, term_to_binary(Reply)),
			loop(Socket);
		{tcp_closed, Socket} ->
			io:format("Server socket closed~n"),
			exit(normal)
	end.